package it.mozzicato.apkwizard;

import it.mozzicato.apkwizard.dto.*;
import it.mozzicato.apkwizard.model.*;
import it.mozzicato.apkwizard.utils.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.BadLocationException;

import org.apache.commons.io.*;

// Creato il 15/mar/2011

/**
 * @author Roberto Mozzicato
 *
 */
public class ResourceDialog extends JDialog {
	private static final int MAX_FIELD_HEIGHT = 26;

	private static final String PROP_LAST_LANGUAGE = "lastLanguage";
	protected static final String PROP_RES_DLG_POSITION = "resourceDlgPos";

	private static final SortableLocale DEFAULT_LANGUAGE = new SortableLocale(Locale.ROOT);

	private static SortableLocale[] locales;

//	private Map<String, List<BaseResource>> insertedResources = new HashMap<String, List<BaseResource>>();
//	private Map<String, List<BaseResource>> updatedResources = new HashMap<String, List<BaseResource>>();
//	private Map<String, List<BaseResource>> deletedResources = new HashMap<String, List<BaseResource>>();

	private JComboBox cmbType;
	private JTextField txtFilter;
	private JTable tblResources;
	private JTable tblValues;
	private JComboBox cmbLanguage;
//	private JButton btnSave;
	private JButton btnClose;
	private String projectName;

	private PublicResourceTableModel resourcesTableModel;
	private ValuesTableModel valuesTableModel;

	private JButton btnAddLanguage;
	private JButton btnRemoveLanguage;
	private JButton btnRevertLanguage;

	private JButton btnAddResource;
	private JButton btnRemoveResource;
	private JButton btnRevertResource;

	private JButton btnAddValue;
	private JButton btnRemoveValue;
	private JButton btnRevertValue;

	private SortableLocale[] languageDirs;

	static {
		Locale tmpArray[] = SimpleDateFormat.getAvailableLocales();
		locales = new SortableLocale[tmpArray.length];
		for (int i = 0; i < tmpArray.length; i++) {
			locales[i] = new SortableLocale(tmpArray[i]);
		}
		Arrays.sort(locales);
	}
	
	/**
	 * Costruttore
	 */
	public ResourceDialog(JFrame parent, String projectName) {
		super(parent, "Resource Editor");

		this.projectName = projectName;

		JLabel lblType=new JLabel("Type: ");
		cmbType = createComboBox(170);
		for (ResourceTypes type : ResourceTypes.values()) {
			cmbType.addItem(new TypeItem(type, new Integer(0)) {
				@Override
				public String toString() {
					return getType() + " (" + getCount() + ')';
				}
			});
		}
		
		JLabel lblFilter=new JLabel("Filter: ");
		
		txtFilter = createTextField();
		
		tblResources = createResourceMasterTable();
		tblValues = createResourceValuesTable();

		JLabel lblLanguage=new JLabel("Subdir (language): ");
		cmbLanguage = createComboBox(300);

		SortableLocale[] languageDirs = getLanguageDirs();
		
		cmbLanguage.addItem(DEFAULT_LANGUAGE);
		for (SortableLocale dir: languageDirs) {
			cmbLanguage.addItem(dir);
		}
		cmbLanguage.setSelectedItem(Configuration.getProperty(PROP_LAST_LANGUAGE));
		
		updateTypeQuantities();
		
		btnAddLanguage = createButton("", ApkWizard.getIcon("add"));
		btnRemoveLanguage = createButton("", ApkWizard.getIcon("delete"));
		btnRevertLanguage = createButton("", ApkWizard.getIcon("undo"));
		btnAddResource = createButton("", ApkWizard.getIcon("add"));
		btnRemoveResource = createButton("", ApkWizard.getIcon("delete"));
		btnRevertResource = createButton("", ApkWizard.getIcon("undo"));
		btnAddValue = createButton("", ApkWizard.getIcon("add"));
		btnRemoveValue = createButton("", ApkWizard.getIcon("delete"));
		btnRevertValue = createButton("", ApkWizard.getIcon("undo"));
		
//		btnSave = createButton("Save", 70);
//		btnSave.setEnabled(false);
		btnClose = createButton("Close", ApkWizard.getIcon("close"));
//		btnClose = createButton("Close", null);
		
		// Compose
		Box firstRow=Box.createHorizontalBox();
		firstRow.add(Box.createRigidArea(new Dimension(5,1)));
		firstRow.add(lblType);
		firstRow.add(cmbType);
		firstRow.add(Box.createRigidArea(new Dimension(20,1)));
		firstRow.add(lblFilter);
		firstRow.add(txtFilter);
		firstRow.add(Box.createHorizontalGlue());
		firstRow.add(Box.createRigidArea(new Dimension(5,1)));
		
		Box secondRow=Box.createHorizontalBox();
		JScrollPane spRes = new JScrollPane(tblResources);
		spRes.setPreferredSize(new Dimension(500, 200));
		secondRow.add(spRes);
		Box resButtons = Box.createVerticalBox();
		resButtons.add(btnAddResource);
		resButtons.add(Box.createRigidArea(new Dimension(1,2)));
		resButtons.add(btnRemoveResource);
		resButtons.add(Box.createRigidArea(new Dimension(1,2)));
		resButtons.add(btnRevertResource);
		resButtons.add(Box.createVerticalGlue());
		secondRow.add(Box.createRigidArea(new Dimension(2,1)));
		secondRow.add(resButtons);
		
		Box thirdRow=Box.createHorizontalBox();
		thirdRow.add(Box.createRigidArea(new Dimension(5,1)));
		thirdRow.add(lblLanguage);
		thirdRow.add(cmbLanguage);
		thirdRow.add(Box.createRigidArea(new Dimension(2,1)));
		thirdRow.add(btnAddLanguage);
		thirdRow.add(Box.createRigidArea(new Dimension(2,1)));
		thirdRow.add(btnRemoveLanguage);
		thirdRow.add(Box.createRigidArea(new Dimension(2,1)));
		thirdRow.add(btnRevertLanguage);
		thirdRow.add(Box.createHorizontalGlue());
		thirdRow.add(Box.createRigidArea(new Dimension(5,1)));

		Box fourthRow=Box.createHorizontalBox();
		JScrollPane spValues = new JScrollPane(tblValues);
		spValues.setPreferredSize(new Dimension(500, 200));
		fourthRow.add(spValues);
		Box valButtons = Box.createVerticalBox();
		valButtons.add(btnAddValue);
		valButtons.add(Box.createRigidArea(new Dimension(1,2)));
		valButtons.add(btnRemoveValue);
		valButtons.add(Box.createRigidArea(new Dimension(1,2)));
		valButtons.add(btnRevertValue);
		valButtons.add(Box.createVerticalGlue());
		fourthRow.add(Box.createRigidArea(new Dimension(2,1)));
		fourthRow.add(valButtons);

		Box lastRow=Box.createHorizontalBox();
		lastRow.add(Box.createHorizontalGlue());
//		lastRow.add(btnSave);
//		lastRow.add(Box.createRigidArea(new Dimension(2,1)));
		lastRow.add(btnClose);
		lastRow.add(Box.createRigidArea(new Dimension(5,1)));

		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		getContentPane().add(Box.createVerticalStrut(5));
		getContentPane().add(firstRow);
		getContentPane().add(Box.createVerticalStrut(5));
		getContentPane().add(secondRow);
		getContentPane().add(Box.createVerticalStrut(5));
		getContentPane().add(thirdRow);
		getContentPane().add(Box.createVerticalStrut(5));
		getContentPane().add(fourthRow);
		getContentPane().add(Box.createVerticalStrut(5));
		getContentPane().add(lastRow);
		getContentPane().add(Box.createVerticalStrut(5));
		
		setMinimumSize(new Dimension(600,300));
		setMaximumSize(new Dimension(800,600));
		//setSize(500, 400);
		pack();
		int[] pos = Configuration.getPropertyAsIntArray(PROP_RES_DLG_POSITION);
		if(pos != null) {
			if(pos.length >= 2)
				setLocation(pos[0], pos[1]);

			if(pos.length >= 4)
				setSize(pos[2], pos[3]);
		}
		
		createHandlers();
	}

	private SortableLocale[] getLanguageDirs() {
		if(languageDirs == null) {
			File resourceDir = PathUtils.getFileInProjects(projectName, ApkWizard.SUBPATH_DECOMP, "res");
			File[] valuesDirs = resourceDir.listFiles(new FileFilter() {
				Pattern pShort = Pattern.compile("values-[a-z]{2}");
				Pattern pFull = Pattern.compile("values-[a-z]{2}-r[A-Z]{2}");
	
				@Override
				public boolean accept(File pathname) {
					return pathname.isDirectory() && (pShort.matcher(pathname.getName()).matches() || pFull.matcher(pathname.getName()).matches());
				}
			});
			
			languageDirs = new SortableLocale[valuesDirs.length];
			for (int i = 0; i < valuesDirs.length; i++) {
				String localeName = valuesDirs[i].getName().substring("values-".length());
				if(localeName.length() >= 6)
					languageDirs[i] = new SortableLocale(new Locale(localeName.substring(0,2), localeName.substring(4)));
				else
					languageDirs[i] = new SortableLocale(new Locale(localeName.substring(0,2)));
				
				languageDirs[i].setDirectory(valuesDirs[i]);
				File rollbackDir = PathUtils.getFileInProjects(projectName, ApkWizard.SUBPATH_BACKUP, "res", valuesDirs[i].getName() + ApkWizard.ROLLBACK_SUFFIX);
				if(rollbackDir.exists())
					languageDirs[i].setStatus(ResourceStatus.INSERTED);
			}
			Arrays.sort(languageDirs);
		}
		return languageDirs;
	}

	private void updateTypeQuantities() {
		for (int i=0; i < cmbType.getItemCount(); i++) {
			TypeItem typeEntry = (TypeItem) cmbType.getItemAt(i);
			typeEntry.setCount(resourcesTableModel.getResourceTypeCount(typeEntry.getType()));
		}
	}

	private static JButton createButton(String text, Icon icon) {
		JButton ret = new JButton(text);
		if(icon != null)
			ret.setIcon(icon);
//		ret.setMargin(new Insets(0,0,0,0));
//		ret.setMinimumSize(new Dimension(width, MAX_FIELD_HEIGHT));
//		ret.setPreferredSize(new Dimension(width, MAX_FIELD_HEIGHT));
//		ret.setMaximumSize(new Dimension(width, MAX_FIELD_HEIGHT));
		return ret;
	}

	private JComboBox createComboBox(int preferredWidth) {
		JComboBox ret = new JComboBox();
		ret.setRenderer(new StatusRenderer());
		ret.setMinimumSize(new Dimension(100, MAX_FIELD_HEIGHT));
		ret.setPreferredSize(new Dimension(preferredWidth, MAX_FIELD_HEIGHT));
		ret.setMaximumSize(new Dimension(400, MAX_FIELD_HEIGHT));
		return ret;
	}

	private JTextField createTextField() {
		JTextField ret = new JTextField();
		ret.setMinimumSize(new Dimension(100, MAX_FIELD_HEIGHT));
		ret.setPreferredSize(new Dimension(150, MAX_FIELD_HEIGHT));
		ret.setMaximumSize(new Dimension(400, MAX_FIELD_HEIGHT));
		return ret;
	}

	private JTable createResourceMasterTable() {
		resourcesTableModel = new PublicResourceTableModel(projectName);
		resourcesTableModel.setActiveType(((TypeItem) cmbType.getSelectedItem()).getType());
		JTable ret = new JTable(resourcesTableModel);
		ret.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		ret.getColumnModel().getColumn(0).setPreferredWidth(90);
		ret.getColumnModel().getColumn(1).setPreferredWidth(250);
		ret.getColumnModel().getColumn(2).setPreferredWidth(300);
		
		ret.setDefaultRenderer(String.class, new StatusRenderer());
		//ret.getColumnModel().getColumn(0).setWidth(70);

//		ret.getColumnModel().getColumn(2).setCellRenderer(ret.getDefaultRenderer(String.class));
//		ret.getColumnModel().getColumn(2).setCellEditor(ret.getDefaultEditor(String.class));

		return ret;
	}

	private JTable createResourceValuesTable() {
		valuesTableModel = new ValuesTableModel(projectName);
		JTable ret = new JTable(valuesTableModel);
		ret.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		ret.getColumnModel().getColumn(0).setPreferredWidth(250);
		ret.getColumnModel().getColumn(1).setPreferredWidth(300);
		
		ret.setDefaultRenderer(String.class, new StatusRenderer());
		return ret;
	}

	private void createHandlers() {
//		btnSave.addActionListener(new ActionListener() {
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				saveResources();
//				ResourceDialog.this.dispose();
//			}
//		});
		
		btnClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Configuration.setProperty(PROP_RES_DLG_POSITION, getX() + "," + getY() + "," + getWidth() + "," + getHeight());
				ResourceDialog.this.dispose();
			}
		});
		
		cmbType.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				resourcesTableModel.setActiveType(((TypeItem) cmbType.getSelectedItem()).getType());
				refreshValuesTable();
			}
		});
		cmbLanguage.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Configuration.setProperty(PROP_LAST_LANGUAGE, cmbLanguage.getSelectedItem().toString());
				refreshValuesTable();
			}
		});
		
		txtFilter.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				changedUpdate(e);
			}
			
			@Override
			public void insertUpdate(DocumentEvent e) {
				changedUpdate(e);
			}
			
			@Override
			public void changedUpdate(DocumentEvent e) {
				try {
					resourcesTableModel.filter(e.getDocument().getText(0, e.getDocument().getLength()));
					refreshValuesTable();
				}
				catch (BadLocationException e1) {
					e1.printStackTrace();
				}
			}
		});
		
		tblResources.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				refreshValuesTable();
			}
		});
		tblResources.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				refreshValuesTable();
			}
		});

		btnAddLanguage.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				SortableLocale language = (SortableLocale) JOptionPane.showInputDialog(
					ResourceDialog.this, 
					"Please select the language to add", 
					"Add language", 
					JOptionPane.INFORMATION_MESSAGE, 
					null, 
					locales,
					null);
				
				if(language != null) {
					addLanguage(language);
				}
			}			
		});

		btnRemoveLanguage.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				if(cmbLanguage.getSelectedIndex() == 0) {
					JOptionPane.showMessageDialog(ResourceDialog.this, "Cannot delete the default language!", "Error", JOptionPane.ERROR_MESSAGE);
					return;
				}

				int choice = JOptionPane.showConfirmDialog(ResourceDialog.this, "Deleting a language all the associated files will be deleted as well.\nAre you sure to continue?", "Warning", JOptionPane.YES_NO_OPTION);
				if(choice == JOptionPane.YES_OPTION) {
					removeLanguage();
				}
			}			
		});

		btnRevertLanguage.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				revertLanguage();
			}			
		});

		btnAddResource.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent event) {
				String name = JOptionPane.showInputDialog(ResourceDialog.this, "Enter new resource name: ", "Resource name", JOptionPane.QUESTION_MESSAGE);
				if(name != null) {
					try {
						PublicResource res = new PublicResource(resourcesTableModel.getActiveType(), name, null);
						resourcesTableModel.addResource(res);
					}
					catch (IOException e) {
						JOptionPane.showMessageDialog(ResourceDialog.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					}
					tblResources.getSelectionModel().setSelectionInterval(tblResources.getRowCount()-1, tblResources.getRowCount()-1);
					tblResources.scrollRectToVisible(tblResources.getCellRect(tblResources.getRowCount()-1, 0, true));
				}
			}
		});

		btnRemoveResource.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent event) {
				if(tblResources.getSelectedRow() <= 0)
					return;
				String id = (String) resourcesTableModel.getValueAt(tblResources.getSelectedRow(), PublicResourceTableModel.columns.ID.ordinal());
				String name = (String) resourcesTableModel.getValueAt(tblResources.getSelectedRow(), PublicResourceTableModel.columns.NAME.ordinal());
				int choice = JOptionPane.showConfirmDialog(ResourceDialog.this, "Deleting a resource all the associated values will be deleted as well.\nAre you sure to continue?", "Warning", JOptionPane.YES_NO_OPTION);
				if(choice == JOptionPane.YES_OPTION) {
					try {
						PublicResource res = new PublicResource(resourcesTableModel.getActiveType(), name, id);
						resourcesTableModel.removeResource(res, getLanguageDirs());
					}
					catch (IOException e) {
						JOptionPane.showMessageDialog(ResourceDialog.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});

		btnAddValue.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				if(tblResources.getSelectedRow() < 0) {
					JOptionPane.showMessageDialog(ResourceDialog.this, 
						"You have to select a resource from the table above!",
						"Unable to add a new value", JOptionPane.ERROR_MESSAGE);
					
					return;
				}
				
				if(valuesTableModel.getRowCount() >= resourcesTableModel.getActiveType().getMaxCount()) {
					JOptionPane.showMessageDialog(ResourceDialog.this, 
						String.format("A resource of type %s can have at most %d values!", 
							resourcesTableModel.getActiveType().getDescription(),
							resourcesTableModel.getActiveType().getMaxCount()),
						"Unable to add a new value", JOptionPane.ERROR_MESSAGE);
					
					return;
				}
				String value = JOptionPane.showInputDialog(ResourceDialog.this, "Enter the new value: ", "Value content", JOptionPane.QUESTION_MESSAGE);
				if(value != null) {
					try {
						ValueResource res = new ValueResource(
							resourcesTableModel.getItemAt(tblResources.getSelectedRow()), 
							(SortableLocale) cmbLanguage.getSelectedItem(), 
							value);
						valuesTableModel.addValue(res);
					}
					catch (IOException e) {
						JOptionPane.showMessageDialog(ResourceDialog.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					}
					tblValues.getSelectionModel().setSelectionInterval(tblValues.getRowCount()-1, tblValues.getRowCount()-1);
					tblValues.scrollRectToVisible(tblValues.getCellRect(tblValues.getRowCount()-1, 0, true));
				}
			}
		});

		btnRemoveValue.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent event) {
				if(tblResources.getSelectedRow() <= 0)
					return;
				int choice = JOptionPane.showConfirmDialog(ResourceDialog.this, "Are you sure you want to delete this value?", "Delete value", JOptionPane.YES_NO_OPTION);
				if(choice == JOptionPane.YES_OPTION) {
					try {
						valuesTableModel.removeValue(valuesTableModel.getItemAt(tblValues.getSelectedRow()), getLanguageDirs());
					}
					catch (IOException e) {
						JOptionPane.showMessageDialog(ResourceDialog.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
	}

	protected void addLanguage(SortableLocale language) {
		File newDir = PathUtils.getFileInProjects(projectName, ApkWizard.SUBPATH_DECOMP, "res", language.getDirectory().getName());
		if(!newDir.exists() && !newDir.mkdir()) {
			JOptionPane.showMessageDialog(this, "Unable to create directory " + newDir.getAbsolutePath(), "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		File rollbackDir = PathUtils.getFileInProjects(projectName, ApkWizard.SUBPATH_BACKUP, "res", language.getDirectory().getName() + ApkWizard.ROLLBACK_SUFFIX);
		if(!rollbackDir.exists() && !rollbackDir.mkdirs()) {
			JOptionPane.showMessageDialog(this, "Unable to create directory " + rollbackDir.getAbsolutePath(), "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		language.setStatus(ResourceStatus.INSERTED);
		cmbLanguage.addItem(language);
		cmbLanguage.setSelectedIndex(cmbLanguage.getItemCount()-1);
	}

	protected void removeLanguage() {
		SortableLocale language = (SortableLocale) cmbLanguage.getSelectedItem();

		File langDir = PathUtils.getFileInProjects(projectName, ApkWizard.SUBPATH_DECOMP, "res", language.getDirectory().getName());
		if(!langDir.exists()) {
			JOptionPane.showMessageDialog(this, "Unable to find directory " + langDir.getAbsolutePath(), "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		File rollbackDir = PathUtils.getFileInProjects(projectName, ApkWizard.SUBPATH_BACKUP, "res", language.getDirectory().getName() + ApkWizard.ROLLBACK_SUFFIX);
		if(rollbackDir.exists()) {
			// If we are deleting a language which didn't exist in the original package, simply remove the directory and it's rollback placeholder
			try {
				FileUtils.deleteDirectory(rollbackDir);

			}
			catch (IOException e) {
				JOptionPane.showMessageDialog(this, "Unable to delete directory " + rollbackDir.getAbsolutePath(), "Error", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
				return;
			}

			try {
				FileUtils.deleteDirectory(langDir);
			}
			catch (IOException e) {
				JOptionPane.showMessageDialog(this, "Unable to delete directory " + rollbackDir.getAbsolutePath(), "Error", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
				return;
			}
		}
		else {
			// If we are deleting a language which existed, first copy any backup file back to the main dir, 
			// then delete the backup dir and move the main dir to the backup location
			File backupDir = PathUtils.getFileInProjects(projectName, ApkWizard.SUBPATH_BACKUP, "res", language.getDirectory().getName());
			if(backupDir.exists()) {
				try {
					FileUtils.copyDirectory(backupDir, langDir, new FileFilter() {
						
						@Override
						public boolean accept(File pathname) {
							return !pathname.getName().endsWith(ApkWizard.ROLLBACK_SUFFIX);
						}
					}, true);
				}
				catch (IOException e) {
					JOptionPane.showMessageDialog(this, "Unable to restore backup files from directory " + backupDir.getAbsolutePath(), "Error", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
					return;
				}
				
				try {
					FileUtils.deleteDirectory(backupDir);
				}
				catch (IOException e) {
					JOptionPane.showMessageDialog(this, "Unable to delete backup directory " + backupDir.getAbsolutePath(), "Error", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
					return;
				}
			}
			
			try {
				FileUtils.moveDirectory(langDir, backupDir);
			}
			catch (IOException e) {
				JOptionPane.showMessageDialog(this, "Unable to move backup directory " + langDir.getAbsolutePath() + " to " + backupDir.getAbsolutePath(), "Error", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
				return;
			}
		}
		
		cmbLanguage.removeItemAt(cmbLanguage.getSelectedIndex());
		cmbLanguage.setSelectedIndex(0);		
	}

	protected void revertLanguage() {
		
	}

	protected void refreshValuesTable() {
		if(tblResources.getSelectedRow() < 0) {
			valuesTableModel.loadValues(null, null);
		}
		else {
			valuesTableModel.loadValues(
				resourcesTableModel.getItemAt(tblResources.getSelectedRow()),
				(SortableLocale) cmbLanguage.getSelectedItem());
		}		
	}
}
